package lims.client;

import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.Serializable;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LimsPrinter implements Serializable{
	/**
	 * 
	 */
	private static Log logger=LogFactory.getLog(LimsPrinter.class);
	private static final long serialVersionUID = -1604379603494771488L;
	public String name;
	public String serviceName;
	public PrintRequestAttributeSet attributes;
	public String description;
	public PageFormat pageFormat=PrinterJob.getPrinterJob().defaultPage();
	public double scale=1.0;
	
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeObject("start LimsPrinter");
		out.writeUTF(name);
		out.writeUTF(serviceName);
		out.writeObject(attributes);
		out.writeUTF(description);		
		out.writeDouble(scale);
		out.writeObject("finish LimsPrinter");
	}
	
	private void readObject(java.io.ObjectInputStream in)  throws IOException, ClassNotFoundException{
		if(!in.readObject().equals("start LimsPrinter")) throw new IOException("flag1 error");
		name=in.readUTF();
		serviceName=in.readUTF();
		attributes=(PrintRequestAttributeSet) in.readObject();
		description=in.readUTF();
		scale=in.readDouble();
		if(!in.readObject().equals("finish LimsPrinter")) throw new IOException("flag1 error");
	}
	
	
	public LimsPrinter(){
		this("_未命名",PrinterJob.getPrinterJob().getPrintService(),new HashPrintRequestAttributeSet(),"");
	}
	public LimsPrinter(String name,PrintService service,PrintRequestAttributeSet attributes,String description){
		this.name=name;
		if(service!=null) this.serviceName=service.getName();
		this.attributes=attributes;
		this.description=description;
	}
	
	public PrintService getPrintService(){
		logger.info(String.format("查找输出设备:%s",serviceName));
		logger.info(String.format("列出所有输出设备："));
		for(PrintService service:PrinterJob.lookupPrintServices()){//PrintServiceLookup.lookupPrintServices(null,null)){
			logger.debug(String.format("输出设备:%s",service.getName()));
		}
		logger.info(String.format("列举完毕"));
/*		logger.info(String.format("列出所有符合条件的输出设备："));
		for(PrintService service:PrintServiceLookup.lookupPrintServices(null,attributes)){
			logger.debug(String.format("符合输出设备:%s",service.getName()));
		}
		logger.info(String.format("列举完毕"));
	*/	
		for(PrintService service:PrinterJob.lookupPrintServices()){//PrintServiceLookup.lookupPrintServices(null,null)){
			logger.debug(String.format("输出设备:%s",service.getName()));
			if(service.getName().equals(serviceName)){				
//				OrientationRequested orientation=(OrientationRequested) attributes.get(OrientationRequested.class);
//		/*
//				Media media=(Media)attributes.get(Media.class);
//			
//				MediaSize mediaSize=null;//MediaSize.getMediaSizeForName( (media.getName());*/
//				MediaPrintableArea printableArea=(MediaPrintableArea)attributes.get(MediaPrintableArea.class);
//	
////				float docWidth = printableArea.getWidth(MediaPrintableArea.INCH)*75;
////		    	float docHeight = printableArea.getHeight(MediaPrintableArea.INCH)*75;
//		    	
//		    	Paper paper = new Paper();
//		    	paper.setSize(printableArea.getX(MediaSize.INCH)*75+printableArea.getWidth(MediaSize.INCH)*75,printableArea.getY(MediaSize.INCH)*75+printableArea.getHeight(MediaSize.INCH)*75);    	
//		    	paper.setImageableArea(printableArea.getX(MediaPrintableArea.INCH)*75,printableArea.getY(MediaPrintableArea.INCH)*75,printableArea.getWidth(MediaPrintableArea.INCH)*75,printableArea.getHeight(MediaPrintableArea.INCH)*75);    	
//		    	
//		    	PageFormat pageFormat = new PageFormat();
//		    	if(orientation.getValue()==OrientationRequested.PORTRAIT.getValue()) pageFormat.setOrientation(PageFormat.PORTRAIT);
//		    	else if(orientation.getValue()==OrientationRequested.LANDSCAPE.getValue()) pageFormat.setOrientation(PageFormat.LANDSCAPE);
//		    	else pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
//		    	this.pageFormat=pageFormat;
//		    	pageFormat.
//		    	pageFormat.setPaper(paper);
				logger.debug("输出设备匹配，返回");
		    	return service;
			}else{
				logger.debug("输出设备不匹配，继续下一条");
			}
		}
		return null;
	}
	
	
	public PageFormat getPageFormat() throws PrinterException{
		PrinterJob job=PrinterJob.getPrinterJob();
		job.setPrintService(getPrintService());
		return job.getPageFormat(attributes);
	}
	
	public boolean setUp() throws PrinterException{
		PrinterJob job=PrinterJob.getPrinterJob();
		if(getPrintService()!=null)	job.setPrintService(getPrintService());
		if(job.printDialog(attributes)){
			serviceName=job.getPrintService().getName();			
		}
//		PrintService defaultService=getService();
//		if(defaultService==null) defaultService=PrintServiceLookup.lookupDefaultPrintService();
//		PrintService printService = ServiceUI.printDialog(null, 200, 200, PrintServiceLookup.lookupPrintServices(null,attributes)
//		            , defaultService,null,attributes);
//		if(printService==null) return false;
		return true;
	}
}